package com.example.opengldemo.simpleGame.object;

import android.opengl.GLES30;

import com.bulletphysics.collision.shapes.CollisionShape;
import com.bulletphysics.dynamics.DiscreteDynamicsWorld;
import com.bulletphysics.dynamics.RigidBody;
import com.bulletphysics.dynamics.RigidBodyConstructionInfo;
import com.bulletphysics.linearmath.DefaultMotionState;
import com.bulletphysics.linearmath.Transform;
import com.example.opengldemo.simpleGame.MatrixState.MatrixState3D;

import java.nio.ByteBuffer;
import java.nio.ByteOrder;
import java.nio.FloatBuffer;

import javax.vecmath.Vector3f;

public class TexFloor {
	
	int mProgram;
    int muMVPMatrixHandle;
    int muMMatrixHandle;
    int maTexCoorHandle;
    int uTexHandle;
    int maCameraHandle;
    int maPositionHandle;
	
	private FloatBuffer mVertexBuffer;
    private FloatBuffer   mTextureBuffer;
    
    int vCount;
    float yOffset;
    
    public TexFloor(int mProgram, final float UNIT_SIZE, float xOffset, float yOffset, float zOffset, CollisionShape groundShape, DiscreteDynamicsWorld dynamicsWorld, float angle)
    {
    	this.mProgram=mProgram;
    	this.yOffset=yOffset;

		Transform groundTransform = new Transform();
		groundTransform.setIdentity();
		groundTransform.basis.rotZ(angle);
		groundTransform.origin.set(new Vector3f(xOffset, yOffset, zOffset));		
		Vector3f localInertia = new Vector3f(0, 0, 0);

		DefaultMotionState myMotionState = new DefaultMotionState(groundTransform);

		RigidBodyConstructionInfo rbInfo = new RigidBodyConstructionInfo(0, myMotionState, groundShape, localInertia);

		RigidBody body = new RigidBody(rbInfo);

		body.setRestitution(0.4f);

		body.setFriction(0.8f);

		dynamicsWorld.addRigidBody(body);
		initVertexData(UNIT_SIZE);
		intShader(mProgram);
    }

    public void initVertexData(final float UNIT_SIZE){

        vCount=6;
        float vertices[]=new float[]
        {        	
        	1*UNIT_SIZE,yOffset,1*UNIT_SIZE,
        	-1*UNIT_SIZE,yOffset,-1*UNIT_SIZE,
        	-1*UNIT_SIZE,yOffset,1*UNIT_SIZE,
        	
        	1*UNIT_SIZE,yOffset,1*UNIT_SIZE,
        	1*UNIT_SIZE,yOffset,-1*UNIT_SIZE,
        	-1*UNIT_SIZE,yOffset,-1*UNIT_SIZE,        	
        };
		


        ByteBuffer vbb = ByteBuffer.allocateDirect(vertices.length*4);
        vbb.order(ByteOrder.nativeOrder());
        mVertexBuffer = vbb.asFloatBuffer();
        mVertexBuffer.put(vertices);
        mVertexBuffer.position(0);

        float textures[]=new float[]
        {
        	UNIT_SIZE/2,UNIT_SIZE/2,  0,0,  0,UNIT_SIZE/2,
        	UNIT_SIZE/2,UNIT_SIZE/2,  UNIT_SIZE/2,0,  0,0
        };


        ByteBuffer tbb = ByteBuffer.allocateDirect(textures.length*4);
        tbb.order(ByteOrder.nativeOrder());
        mTextureBuffer= tbb.asFloatBuffer();
        mTextureBuffer.put(textures);
        mTextureBuffer.position(0);
    }

    public void intShader(int mProgram)
    {

        maPositionHandle = GLES30.glGetAttribLocation(mProgram, "aPosition");

        maTexCoorHandle=GLES30.glGetAttribLocation(mProgram, "aTexCoor");  

        muMVPMatrixHandle = GLES30.glGetUniformLocation(mProgram, "uMVPMatrix");   

        muMMatrixHandle = GLES30.glGetUniformLocation(mProgram, "uMMatrix");  

        maCameraHandle=GLES30.glGetUniformLocation(mProgram, "uCamera"); 
        uTexHandle=GLES30.glGetUniformLocation(mProgram, "sTexture"); 
    }
    
    public void drawSelf(int texId) 
    {        

    	 GLES30.glUseProgram(mProgram);

         GLES30.glUniformMatrix4fv(muMVPMatrixHandle, 1, false, MatrixState3D.getFinalMatrix(), 0);

         GLES30.glUniformMatrix4fv(muMMatrixHandle, 1, false, MatrixState3D.getMMatrix(), 0);  

         GLES30.glUniform3fv(maCameraHandle, 1, MatrixState3D.cameraFB);
         

         GLES30.glVertexAttribPointer        
         (
         		maPositionHandle,   
         		3, 
         		GLES30.GL_FLOAT, 
         		false,
                3*4, 
                mVertexBuffer   
         );       

         GLES30.glVertexAttribPointer  
         (  
        		maTexCoorHandle,  
         		2, 
         		GLES30.GL_FLOAT, 
         		false,
                2*4,   
                mTextureBuffer
         );   

         GLES30.glEnableVertexAttribArray(maPositionHandle);  
         GLES30.glEnableVertexAttribArray(maTexCoorHandle);  

         GLES30.glActiveTexture(GLES30.GL_TEXTURE0);
         GLES30.glBindTexture(GLES30.GL_TEXTURE_2D, texId);    
         GLES30.glUniform1i(uTexHandle, 0);
           

         GLES30.glDrawArrays(GLES30.GL_TRIANGLES, 0, vCount); 
    }
}
